package no.ntnu.fp.gui.chat;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ChatWindow extends JPanel {
	private JTextArea chatArea;
	private JTextField inputField;
	private JList chatClients;
	private DefaultListModel listModel;
	private int width;
	private int heigth;
	private int chatAreaHeight;
	private int chatAreaWidth;
	private JButton sendButton;
	
	public ChatWindow() {
		width = 800;
		heigth = 600;
		chatAreaHeight = 550;
		chatAreaWidth = 600;
		// Configure the Panel.
		setPreferredSize(new Dimension(width,heigth));
		
		// Configure ChatArea
		chatArea = new JTextArea();
		chatArea.setPreferredSize(new Dimension(chatAreaWidth,chatAreaHeight));
		chatArea.setEditable(false);
		chatArea.setLineWrap(true);
		
		// Confingure InputField
		inputField = new JTextField();
		inputField.setColumns(60);
		
		//Configure ClientList
		listModel = new DefaultListModel();
		chatClients = new JList(listModel);
		listModel.setSize(30);
		chatClients.setPreferredSize(new Dimension(100, 550));
		
		
		// Configure JButton
		sendButton = new JButton();
		sendButton.setText("Send");
		
		// Adding Douchebag layout and Constraints
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		c.gridheight = 2;
		add(chatArea, c);
		
		c.gridx = 0;
		c.gridy = 2;
		c.gridheight = 0;
		add(inputField, c);
		
		c.gridx = 2;
		c.gridy = 0;
		c.gridwidth = 0;
		c.gridheight = 2;
		add(chatClients,c);
		
		c.gridx = 2;
		c.gridy = 2;
		c.gridwidth = 0;
		c.gridheight = 0;
		add(sendButton,c);
		
		// Add ActionListeners
		sendButton.addActionListener(new SendButtonListener());
		
	
	}
	/**
	 * Should add users that are chatting into
	 * the JList.
	 *  NOT IMPLEMENTED
	 * @author ---
	 */
	public void addChatUser() {
		listModel.addElement("CHAT USER");
	}
	
	/**
	 * Should remove a user when it disconnects
	 *  NOT IMPLEMENTED
	 * @author ---
	 */
	public void removeChatUser() {
		
	}
	/**
	 * Should add and replace chatlines at the right place
	 * @param chatLine
	 * @param user
	 */
	public void addLineToChat(String chatLine, String user, boolean priv){
			if (priv) chatArea.insert("[PRIVATE]"+user+": "+chatLine+"\n", 0);
			else chatArea.insert(user+": "+chatLine+"\n", 0);
	
	}

	class SendButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (!inputField.getText().equals(""))
			addLineToChat(inputField.getText(), "me", false);
			
		}
		
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame("TEst");
		ChatWindow panel = new ChatWindow();
		frame.setContentPane(panel);
		frame.pack();
		frame.setVisible(true);
		
	}
}
